/**
 * Created by wry on 2015/3/17.
 */
'use strict';

angular.module('backofficeApp.promotionCode.controllers', []).
    controller('promotionCodeCtrl', ['$scope', '$location', '$http', '$interval', function ($scope, $location, $http, $interval) {

        $scope.$on('$viewContentLoaded', function () {
            $http.get('http://' + backOfficeHost + '/rest/v1.0/backoffice/Systems/getPromotionCodeDateCreated', {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (result) {
                $scope.dateList = result.data.createDate;
                $scope.expirationDateList = result.data.expirationDate;
            }).error(function () {
            });
        });

        $('#expirationDate').datepicker().on('changeDate', function (ev) {
            $scope.expirationDate = ev.date;
        });


        $scope.generateCode = function (length) {
            if ($scope.expirationDate == null || length == null) {
                return;
            }

            var input = {
                number: Number(length),
                expirationDate: $scope.expirationDate
            };

            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/Systems/generatePromotionCode', input, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (result) {
                $http.get('http://' + backOfficeHost + '/rest/v1.0/backoffice/Systems/getPromotionCodeDateCreated', {
                    headers: {'Content-Type': 'application/json;charset=utf-8'}
                }).success(function (result) {
                    $scope.dateList = result.data;
                }).error(function () {
                });
                $scope.searchCode();
            }).error(function () {
            });
        };

        $scope.getPromotionCodeList = function (input) {

            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/Systems/getPromotionCodeList', input, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (result) {
                $scope.codes = result.data;
            }).error(function () {
            });
        };

        var option = {};

        $scope.searchCode = function (input) {
            var opt = {};
            if (input != null && input.dateCreated != null && input.dateCreated != '') {
                opt.dateCreated = input.dateCreated;
            }
            if (input != null && input.expirationDate != null && input.expirationDate != '') {
                opt.expirationDate = input.expirationDate;
            }
            if (input != null && input.owner != null && input.owner != '') {
                opt.owner = input.owner;
            }
            option = opt;
            $scope.getPromotionCodeList(opt);
        };

        $scope.searchCode();

        $scope.setOwner = function (id) {
            bootbox.prompt('设置拥有者', function (result) {
                if (result != null && result != '') {
                    var opt = {
                        objId: id,
                        owner: result
                    };
                    $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/Systems/setPromotionCodeOwner', opt, {
                        headers: {'Content-Type': 'application/json;charset=utf-8'}
                    }).success(function (result) {
                        $scope.searchCode(option);
                    }).error(function () {
                    });
                }
            }).find("div.modal-dialog").css({width: '30%'});
        }

    }]);